;(function() {
  new Vue({
    el: '#finicial_voucher_rule_config_one',
    data: {
      bizCategoryCode:null,
      bizCategoryName:null,
      bizTypeName:null,
      voucherRules:[],
      accountingLists:[],
      selectedAccountId:null,
      ops: { bar: { background: '#c1c1c1'} },
    },
    mounted(){
      this.getAccountingListData()
    },
    methods: {
      // 获取帐套名称列表
      getAccountingListData(){
        var self = this
        hlhttp.get({
          url:'acct/accountings',
          success(data){
            self.accountingLists = data
            if(data.length){
              self.selectedAccountId = sessionStorage.getItem("accountId")
              self.getVoucherRulesData()
            }
          }
        })
      },
      selectAccounting(id,name){
        this.selectedAccountId = id
        sessionStorage.setItem("accountId", this.selectedAccountId);
        this.getVoucherRulesData()
      },
      // 获取凭证规则下的业务类型
      getVoucherRulesData(){
        var self = this
        hlhttp.get({
          url:'acct/voucher_rules',
          data:{'accounting_id':self.selectedAccountId},
          success(data){
            self.voucherRules = data
            if(self.voucherRules[0]['rules'].length){
              self.bizCategoryCode = self.voucherRules[0]['rules'][0]['bizCategoryCode']
              self.bizCategoryName = self.voucherRules[0]['rules'][0]['name']
            }else{
              self.bizCategoryCode = self.voucherRules[0].bizCategoryCode
              self.bizCategoryName = self.voucherRules[0].bizCategoryName
            }
          }
        })
      },
      changeVoucherRuleState(id,state){
        var self = this
        hlhttp.put({
          url:'acct/voucher_rule/_status',
          data:{ 'id':id ,'opt': state == 'Y' ? 'N' : 'Y '},
          success(data){
            self.getVoucherRulesData()
            hl.toast('凭证规则状态改变成功')
          }
        })
      },
      deleteVoucherRule(id){
        var self = this
        hllayer.confirm('确认删除凭证规则？删除之后不可恢复',
            function () {
              hlhttp.delete({
                url:'acct/voucher_rule?id=' + id,
                success(data){
                  self.getVoucherRulesData()
                  hl.toast('凭证规则删除成功')
                }
              })
            }
        )
      },
      addVoucherRule(code,name){
        this.bizCategoryCode = code
        this.bizCategoryName = name
        hlUrlUtil.addURLParam({'bizCategoryCode':escape(code),'bizCategoryName':escape(name),'accountId':escape(this.selectedAccountId) })
        window.localStorage.removeItem('voucherRulesId')
        $('.finicial-doc-view-content').load('modules/system_config/page/finicial_voucher_rule_config_two.html')
      },
      setVoucherRule(code,name,rulesId){
        this.bizCategoryCode = code
        this.bizCategoryName = name
        hlUrlUtil.addURLParam({'bizCategoryCode':escape(code),'voucherRulesId':escape(rulesId),'accountId':escape(this.selectedAccountId) })
        window.localStorage.setItem('voucherRulesId',rulesId)
        $('.finicial-doc-view-content').load('modules/system_config/page/finicial_voucher_rule_config_two.html')
      }

    }


  })
}())
